package com.example.metesting;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class FloatingButton extends Service implements OnTouchListener,
		OnClickListener, OnLongClickListener {

	private LinearLayout buttonLayout;
	private ImageView buttonView;
	private WindowManager wm;
	private int initialX;
	private int initialY;
	private float initialTouchX;
	private float initialTouchY;
	private boolean isMoving;
	private WindowManager.LayoutParams params;

	private Timer mButtonAnimationTimer;
	private ButtonAnimationTimerTask mButtonTimerTask;
	private Handler mAnimationHandler = new Handler();

	int screenHeight;
	int screenWidth;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();

		Toast.makeText(this, "Hold button to close.", Toast.LENGTH_SHORT)
				.show();
		// Close the notification drawer after the button showed up
		Intent drawerCloseIntent = new Intent(
				Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
		this.sendBroadcast(drawerCloseIntent);

		screenHeight = getResources().getDisplayMetrics().heightPixels - 50;
		screenWidth = getResources().getDisplayMetrics().widthPixels;

		// Set up the button layout
		buttonLayout = new LinearLayout(this);
		buttonView = new ImageView(this);
		
		buttonView.setImageResource(R.drawable.ic_melogo);
		buttonLayout.setOnTouchListener(this);
		buttonLayout.setOnClickListener(this);
		buttonLayout.setOnLongClickListener(this);
		buttonLayout.addView(buttonView);

		wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		params = new WindowManager.LayoutParams(
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
				WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
						| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
				PixelFormat.TRANSLUCENT);
		params.gravity = Gravity.TOP | Gravity.START;
		params.alpha = 0.6f;
		params.x = (int) (screenWidth * 0.9);
		params.y = (int) (screenHeight * 0.6);

		wm.addView(buttonLayout, params);

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (buttonLayout != null)
			wm.removeView(buttonLayout);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			isMoving = false;
			initialX = params.x;
			initialY = params.y;
			initialTouchX = event.getRawX();
			initialTouchY = event.getRawY();

			params.alpha = 1f;
			wm.updateViewLayout(buttonLayout, params);

			return false;

		case MotionEvent.ACTION_UP:

			params.alpha = 0.6f;
			wm.updateViewLayout(buttonLayout, params);

			// Button stick to the side automatically
			mButtonTimerTask = new ButtonAnimationTimerTask();
			mButtonAnimationTimer = new Timer();
			mButtonAnimationTimer.schedule(mButtonTimerTask, 200, 10);

			if (initialTouchX == event.getRawX()
					&& initialTouchY == event.getRawY())
				return false;
			else
				return true;

		case MotionEvent.ACTION_MOVE:

			isMoving = true;
			params.x = initialX + (int) (event.getRawX() - initialTouchX);
			params.y = initialY + (int) (event.getRawY() - initialTouchY);

			wm.updateViewLayout(buttonLayout, params);
			return true;
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		Toast.makeText(this, "Overlay button click event", Toast.LENGTH_SHORT)
				.show();

		Intent i = new Intent(this, EventCreateActivity.class);
		i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(i);

	}

	@Override
	public boolean onLongClick(View v) {
		if (!isMoving) {
			stopService(new Intent(this, FloatingButton.class));
			Toast.makeText(this, "This is long click", Toast.LENGTH_SHORT)
					.show();

			return true;
		}

		return false;
	}

	private class ButtonAnimationTimerTask extends TimerTask {

		int screenHeight = getResources().getDisplayMetrics().heightPixels - 50;
		int screenWidth = getResources().getDisplayMetrics().widthPixels;
		int buttonHeight = buttonLayout.getHeight();
		int buttonWidth = buttonLayout.getWidth();

		public ButtonAnimationTimerTask() {
			super();
		}

		@Override
		public void run() {

			// handler is used to run the function on main UI thread in order to
			// access the layouts and UI elements.
			mAnimationHandler.post(new Runnable() {
				@Override
				public void run() {

					if (params.y < 5 || params.y > screenHeight - buttonHeight
							|| params.x < 5
							|| params.x > screenWidth - buttonWidth) {
						ButtonAnimationTimerTask.this.cancel();
						mButtonAnimationTimer.cancel();
					} else if (params.y < screenHeight * 0.1) {
						params.y -= 10;
					} else if (params.y > screenHeight * 0.9 - buttonHeight) {
						params.y += 10;
					} else if (params.x < screenWidth / 2 - buttonWidth / 2) {
						params.x -= 10;
					} else if (params.x >= screenWidth / 2 - buttonWidth / 2) {
						params.x += 10;
					}
					wm.updateViewLayout(buttonLayout, params);
				}
			});
		}
	}

}
